package com.shushan.lockscreen;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.android.permission.FloatPermissionUtil;
import com.shushan.ui.CountdownTextView;
import com.shushan.util.ToastUtils;

/**
 * @ClassName: SplashWindowManager
 * @Desciption: 闪屏悬浮窗管理类
 * @author: yichaohua
 * @date: 2018-08-16
 */
public class SplashWindowManager {

    private static final String TAG = "SplashWindowManager";

    private static SplashFloatWindow splashFloatWindow;
    private static WindowManager.LayoutParams splashWindowParams;
    private static WindowManager splashWindowManager;
    private static boolean isSplashAvailable = false;


    public static void createEyeFloatWindow(Context context) {
        Log.i(TAG,"createEyeFloatWindow()");

        if(context == null){
            Log.e(TAG,"context == null");
            return;
        }

        if(splashWindowManager == null){
            splashWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        if(splashWindowParams == null){
            splashWindowParams = new WindowManager.LayoutParams();
        }

        if(splashFloatWindow == null){
            splashFloatWindow = new SplashFloatWindow(context);
        }else {
            Log.i(TAG,"setStartGone()");
            splashFloatWindow.setStartGone();
        }

        splashWindowParams.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//8.0
            splashWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            splashWindowParams.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        splashWindowParams.format = PixelFormat.TRANSPARENT;
        splashWindowParams.gravity = Gravity.TOP;
        splashWindowParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        splashWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        splashWindowParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        //强制竖屏
        splashWindowParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

        if(FloatPermissionUtil.checkPermission(context)){

            if(isSplashAvailable){
                try {
                    splashWindowManager.updateViewLayout(splashFloatWindow, splashWindowParams);
                }catch (IllegalArgumentException e){
                    e.printStackTrace();
                    splashWindowManager.addView(splashFloatWindow, splashWindowParams);
                }

            }else {
                try {
                    splashWindowManager.addView(splashFloatWindow, splashWindowParams);
                }catch (IllegalStateException e){
                    e.printStackTrace();
                    splashWindowManager.removeView(splashFloatWindow);
                    splashWindowManager.addView(splashFloatWindow, splashWindowParams);
                }

            }

//            CountdownTextView countdownTextView = splashFloatWindow.getCountdownTextView();
//            if(countdownTextView != null){
//                countdownTextView.setTimes(5*60);
//            }

            isSplashAvailable = true;
        }else {
            ToastUtils.showToast(context,"书山宝您的手机没有授予悬浮窗权限，请开启后再试",5);
        }
    }

    public static void removeEyeFloatWindow(Context context) {
        Log.i(TAG,"removeEyeFloatWindow()");
        if(splashWindowManager == null){
            splashWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(isSplashAvailable){
            try {
                isSplashAvailable = false;
                splashWindowManager.removeView(splashFloatWindow);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }

//            CountdownTextView countdownTextView = splashFloatWindow.getCountdownTextView();
//            if(countdownTextView != null){
//                countdownTextView.stop();
//            }
        }

    }

    public static void startCount(){
        if(splashFloatWindow != null){
            splashFloatWindow.startCount();
        }
    }
}
